//
//  UIView+SLAddTapShowAnimation.m
//  shunlianPower2
//
//  Created by 陈镇 on 2020/3/28.
//  Copyright © 2020 顺联网络. All rights reserved.
//

#import "SLTapView+SLAddTapShowAnimation.h"

@implementation SLTapView (SLAddTapShowAnimation)


-(UIView *)showHiddenSubView{
    return objc_getAssociatedObject(self, _cmd);
}
-(void)setShowHiddenSubView:(UIView *)showHiddenSubView{
    objc_setAssociatedObject(self, @selector(showHiddenSubView), showHiddenSubView, OBJC_ASSOCIATION_RETAIN);
}

-(BOOL)disableCanClickBackgroundDismiss{
    return [objc_getAssociatedObject(self, _cmd) boolValue];
}
-(void)setDisableCanClickBackgroundDismiss:(BOOL)disableCanClickBackgroundDismiss{
    objc_setAssociatedObject(self, @selector(disableCanClickBackgroundDismiss), @(disableCanClickBackgroundDismiss), OBJC_ASSOCIATION_ASSIGN);

}

-(BOOL)sl_animation{
    return [objc_getAssociatedObject(self, _cmd) boolValue];
}

-(void)setSl_animation:(BOOL)sl_animation{
    objc_setAssociatedObject(self, @selector(sl_animation), @(sl_animation), OBJC_ASSOCIATION_ASSIGN);
}
-(CGFloat)bottomMargin{
    return [objc_getAssociatedObject(self, _cmd) floatValue];
}

-(void)setBottomMargin:(CGFloat)bottomMargin{
    objc_setAssociatedObject(self, @selector(bottomMargin), @(bottomMargin), OBJC_ASSOCIATION_ASSIGN);
}
-(void)sl_addNoResponseTap{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(noResponseClick)];
    tap.delegate = self;
    [self addGestureRecognizer:tap];
    self.userInteractionEnabled = YES;
}

-(void)sl_addDisMissTapAnimationView:(UIView *)subView{
    self.showHiddenSubView = subView;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sl_disMissView_tap)];
    tap.delegate = self;
    [self addGestureRecognizer:tap];
}
-(void)sl_showBottomSubView:(UIView *)subView animation:(BOOL)animation{
    self.showHiddenSubView = subView;
    subView.top = self.superview.height;
    self.sl_animation = animation;
    if ([self respondsToSelector:@selector(sl_AddTapShowAnimationViewWillShow)]) {
        [(id <SLAddTapShowAnimationProtocal>)self sl_AddTapShowAnimationViewWillShow];
    }
    
    if (animation) {
        [UIView animateWithDuration:0.15 animations:^{
            //这个视图移出至父视图的可视范围
            subView.top = self.superview.height - subView.height - self.bottomMargin ;
        }completion:^(BOOL finished) {
            if ([self respondsToSelector:@selector(sl_AddTapShowAnimationViewDidShow)]) {
                [(id <SLAddTapShowAnimationProtocal>)self sl_AddTapShowAnimationViewDidShow];
            }
        }];
    }else{
        subView.top = self.superview.height - subView.height - self.bottomMargin ;
        if ([self respondsToSelector:@selector(sl_AddTapShowAnimationViewDidShow)]) {
            [(id <SLAddTapShowAnimationProtocal>)self sl_AddTapShowAnimationViewDidShow];
        }
    }

    

    

}
-(void)sl_showBottomSubView:(UIView *)subView{
    [self sl_showBottomSubView:subView animation:YES];
}

#pragma makr - 点击事件
-(void)noResponseClick{
    if ([QMUIHelper isKeyboardVisible]) {
        //有键盘的时候记得取消键盘吧
        [self endEditing:NO];
        return;
    }
}
-(void)sl_disMissView_tap{
    if (!self.disableCanClickBackgroundDismiss) {
        [self sl_disMissView];
    }

}
-(void)sl_disMissView{
    if ([QMUIHelper isKeyboardVisible]) {
        //有键盘的时候记得取消键盘吧
        [self endEditing:NO];
    }
    
    if ([self respondsToSelector:@selector(sl_AddTapShowAnimationViewWillHidden)]) {
        [(id <SLAddTapShowAnimationProtocal>)self sl_AddTapShowAnimationViewWillHidden];
    }
    
    if (self.showHiddenSubView && self.sl_animation) {
        [UIView animateWithDuration:0.15 animations:^{
            //这个视图移出至父视图的可视范围
            self.showHiddenSubView.top = self.superview.height;
        }completion:^(BOOL finished) {
            if ([self respondsToSelector:@selector(sl_AddTapShowAnimationViewDidHidden)]) {
                [(id <SLAddTapShowAnimationProtocal>)self sl_AddTapShowAnimationViewDidHidden];
            }
            //父视图移出
            [self removeFromSuperview];
        }];
    }else{
        self.showHiddenSubView.top = self.superview.height;
        if ([self respondsToSelector:@selector(sl_AddTapShowAnimationViewDidHidden)]) {
            [(id <SLAddTapShowAnimationProtocal>)self sl_AddTapShowAnimationViewDidHidden];
        }
        [self removeFromSuperview];
    }

}
#pragma mark tapGestureRecgnizerdelegate 解决手势冲突
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if ([touch.view isKindOfClass:[UITableView class]]){
    return NO;
}
if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
    return NO;
}
return YES;
}

@end


